#ifndef CSV_H

#include <fstream>

#include "utils/String.h"

namespace utils {

class CSV {
public:
  CSV(const std::string &filename, const std::string &delimiter = ",")
      : filename(filename), delimiter(delimiter) {}

  void write(const std::vector<std::vector<std::string>> &data) {
    std::ofstream file(filename);
    for (const auto &row : data) {
      for (size_t i = 0; i < row.size(); i++) {
        file << row[i];
        if (i < row.size() - 1) {
          file << delimiter;
        }
      }
      file << std::endl;
    }
    file.close();
  }

  std::vector<std::vector<std::string>> read() {
    std::vector<std::vector<std::string>> data;
    std::ifstream file(filename);
    std::string line;
    while (std::getline(file, line)) {
      data.push_back(utils::split<std::string>(line, delimiter));
    }
    file.close();
    return data;
  }

private:
  std::string filename;
  std::string delimiter;
};

} // namespace utils

#endif